package com.example.boot.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.LogAnno;
import com.example.common.Result;
import com.example.common.enums.LogType;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.exception.CustomException;
import com.example.pojo.entity.Team;
import com.example.service.TeamService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * TODO
 *
 * @author ding
 * @since 2024/6/2
 */
@RestController
@RequestMapping("/team")
@Slf4j
@Api(tags = "团队相关接口")
public class TeamController {

    @Autowired
    private TeamService teamService;

    /**
     * 新增
     */
    @LogAnno(title = "团队管理", type = LogType.ADD)
//    @PreAuthorize("hasAuthority('TEAM_ADD')")
    @PostMapping("/add")
    public Result add(@RequestBody Team team) {
        teamService.add(team);
        return Result.success();
    }

    /**
     * 删除
     */
    @LogAnno(title = "团队管理", type = LogType.DELETE)
//    @PreAuthorize("hasAuthority('TEAM_DELETE')")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        teamService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @LogAnno(title = "团队管理", type = LogType.DELBATCH)
//    @PreAuthorize("hasAuthority('TEAM_DELETE')")
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        teamService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @LogAnno(title = "团队管理", type = LogType.UPDATE)
//    @PreAuthorize("hasAuthority('TEAM_EDIT')")
    @PutMapping("/update")
    public Result updateById(@RequestBody Team team) {
        teamService.updateById(team);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
//    @PreAuthorize("hasAuthority('TEAM_QUERY')")
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Team team = teamService.selectById(id);
        return Result.success(team);
    }

    /**
     * 通过团队id，获取其团队成员
     */
//    @PreAuthorize("hasAuthority('TEAM_QUERY')")
    @GetMapping("/getUserIds/{id}")
    public Result selectUserById(@PathVariable Integer id) {
       List<Integer> ids = teamService.selectUserById(id);
        return Result.success(ids);
    }

    /**
     * 分页查询
     */
//    @PreAuthorize("hasAuthority('TEAM_PAGEQUERY')")
    @GetMapping("/selectPage")
    public Result selectPage(String name,
                             String projectName,
                             String order,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Team> page = teamService.selectPage(name, projectName, order, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 在团队中删除根据id删除团队成员
     * id: 成员id
     * teamId: 团队id
     */
//    @PreAuthorize("hasAuthority('TEAM_DELPEOPLE')")
    @DeleteMapping("/deleteUsers/{id}/{teamId}")
    public Result deleteUser(@PathVariable Integer id, @PathVariable Integer teamId) {
        if (ObjectUtil.isEmpty(id) || ObjectUtil.isNull(teamId)){
            throw new CustomException(ResultCodeEnum.TEAMID_OR_USERID_ERROR);
        }
        teamService.deleteUser(id, teamId);
        return Result.success();
    }
}
